package unicorns;

import java.util.ArrayList;

import operatingSystems.*;

public class PlayGround {
	ArrayList<Pot> pots; 
	ArrayList<Baby> babies;
	Mother mother;
	int numberOfBabies;
	int numberOfPots;
	int lifeTime;
	Semaphore checkPots;
	Semaphore wakeUpMother;
	Semaphore sleepingMother;
	Barrier willPlay;
	Barrier eating;
	/**
	 * 
	 * just initializing all the variables of the playground
	 * 
	 * @param n the number of babies
	 * @param m the number of pots
	 * @param k the number of runs
	 */
	
	public PlayGround(int n, int m, int k){
		this.numberOfBabies = n;
		this.numberOfPots = m;
		this.lifeTime = k;
		this.checkPots = new Semaphore(1);
		this.wakeUpMother = new Semaphore(1);
		this.sleepingMother = new Semaphore(0);
		this.willPlay = new Barrier(3);
		this.eating = new Barrier(100);
		init();
		run();
		
	}
	
	/**
	 * A method to initialize the pots, mother and the babies all together 
	 */
	
	public void init(){
		
		initPots();
		mother = new Mother(this);
		initBabies();
		
	}
	
	/**
	 * A method to initialize the babies
	 */
	
	public void initBabies() {
		
		babies = new ArrayList<Baby>();
		for(int i = 0; i < numberOfBabies;i++){
			Baby tempBaby = new Baby(this, i);
			babies.add(tempBaby);
		}
		
	}

	
	/**
	 * A method to initialize the pots 
	 */
	public void initPots(){
		
		pots = new ArrayList<Pot>();
		for(int i = 0; i < numberOfPots;i++){
			Pot tempPot = new Pot();
			pots.add(tempPot);
		}
		
	}
	
	/**
	 * A method to loop over the babies and start them
	 */
	public void run(){
		
		mother.start();
		for(Baby baby : babies)
			baby.start();
		
	}
	
	
	
}
